<template>
  <div class="inside-im-box">
    <div class="search_div">
      <el-input
        class="search_input"
        v-model="keywords"
        :placeholder="$t('system.internalCoordination.text2')"
        @input="searchRole"
        clearable
      >
        <i slot="prefix" class="el-input__icon el-icon-search"></i>
      </el-input>
      <i class="iconfont icon-add im-add" @click="change()"></i>
    </div>
    <template v-if="list && list.length > 0">
      <div class="wrap">
        <div
          v-for="(item, index) in list"
          :key="index"
          class="customer-item"
          :class="{ addColor: item.chatUuid == isactive }"
          @click="clickCustomer(item, index)"
        >
          <div v-if="item.chatTop" class="Topping"></div>
          <i class="iconfont icon-close" @click="Operation('Hide', item)"></i>

          <!-- 头像 -->
          <template>
            <img
              class="icon"
              v-if="item.chatTitleHead"
              :src="item.chatTitleHead"
            />
            <img class="icon" v-else :src="insideImImage1" alt="" />
          </template>

          <div class="item-info">
            <div class="im-name">
              <span class="user-name">
                <span>{{ item.chatTitleName }}</span>
                <template v-if="item.errorChat">
                  <span v-if="item.chatType === 1">【{{$t('common.base.deleted')}}】</span>
                  <span v-else>【{{$t('system.internalCoordination.text1')}}】</span>
                </template>
              </span>
              <div class="im-time">
                {{ item.recentTimeStr }}
              </div>
            </div>
            <div class="im-content">
              {{ item.recentMessage }}
            </div>
            <!-- <div class="im-delete" @click.stop="click(item.uuid)">
                    <i class="iconfont icon-fe-close"></i>
                </div> -->
            <div class="Operation">
              <el-dropdown>
                <div class="Operations" @click.stop="">...</div>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-if="!item.chatTop">
                    <div
                      class="OperationLists"
                      @click.stop="Operation('Topping', item)"
                    >
                      {{$t('common.base.topping')}}
                    </div>
                  </el-dropdown-item>
                  <el-dropdown-item v-else>
                    <div
                      class="OperationLists"
                      @click.stop="Operation('CancelTopping', item)"
                    >
                      {{$t('common.base.cancelTopping')}}
                    </div>
                  </el-dropdown-item>
                  <el-dropdown-item>
                    <div
                      class="OperationLists"
                      @click.stop="Operation('Hide', item)"
                    >
                      {{$t('common.base.hide')}}
                    </div>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              <div
                class="im-msg"
                v-if="item.chatUnreadCount != 0"
                :class="getRedDotClass(item.chatUnreadCount)"
              >
                {{ item.chatUnreadCount | unreadCountFilter }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </template>
    <div v-else class="default">
      <div class="default-img">
        <img :src="insideImImage2" alt="" />
      </div>
      <div class="default-msg">{{$t('common.base.tip.noData')}}</div>
    </div>
  </div>
</template>

<script>
import { formatDate } from 'pub-bbx-utils';
import { safeNewDate } from '@src/util/time';
import * as IMApi from '@src/api/ImApi.js';
import { getOssUrl } from '@src/util/assets'
const insideImImage1 = getOssUrl('/avatar.png')
const insideImImage2 = getOssUrl('/im/no_data.png')

export default {
  name: 'inside-im',
  props: {
    list: {
      type: Array,
      default: () => [],
    },
    customerServiceNumber: {
      type: String,
    },
    isactive: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      insideImImage1,
      insideImImage2,
      keywords: '',
      chatItemInfo: {},
    };
  },
  filters: {
    formatDate(value) {
      return formatDate(value, 'YYYY-MM-DD HH:mm:ss');
    },
    unreadCountFilter(value) {
      return value > 99 ? '99+' : value;
    },
    InitializationTime(value) {
      if (typeof value === 'string') {
        let time = value.slice(0, 19);
        // safeNewDate().getTimezoneOffset() 获取的是当前时区 小时 乘 60 分钟的值
        return (
          safeNewDate(time).getTime()
          + safeNewDate().getTimezoneOffset() * 60 * 1000 * -1
        );
      }
      return value;
    },
  },
  methods: {
    formatDate,
      getRedDotClass(num) {
      if (num > 99) return 'im-big-count';

      if (num < 10) return 'im-small-count';

      return 'im-medium-count'
    },
    clickCustomer(item, index) {
      // 用于全局通知判断，记录当前开启页面
      //   sessionStorage.setItem('CurrentCustomerInfor', JSON.stringify(item));
      this.chatItemInfo = item;
      this.$emit('clickCustomer', item, index);
    },
    change() {
      this.$emit('change');
      this.$track.clickStat(this.$track.formatParams('INITIATE_SESSION'));
    },
    searchRole() {
      this.$emit('search', this.keywords);
    },
    click(id) {
      this.$emit('deleteIm', { id });
    },
    Operation(type, item) {
      switch (type) {
      case 'Topping': {
        // 置顶
        IMApi.chatTop({
          chatType: item.chatType,
          chatUuid: item.chatUuid,
        }).then(res => {
          if (res.code === 0) {
            this.$eventBus.$emit('getChatList');
          }
        });
        break;
      }
      case 'CancelTopping': {
        // 取消置顶
        IMApi.chatCancelTop({
          chatType: item.chatType,
          chatUuid: item.chatUuid,
        }).then(res => {
          if (res.code === 0) {
            this.$eventBus.$emit('getChatList');
          }
        });
        break;
      }
      case 'Hide': {
        // 隐藏
        IMApi.chatDisplay({
          chatType: item.chatType,
          chatUuid: item.chatUuid,
        }).then(res => {
          if (res.code === 0) {
            this.$eventBus.$emit('getChatList');
            if(this.chatItemInfo.chatUuid == item.chatUuid){
              this.$eventBus.$emit('closeChat')
            }
          }
        });        
        break;
      }
      default:
        console.log('未知类型');
      }
      const trackObj = {
        Topping: {
          id: 'TOPPING_SESSION',
          data: 'TOP'
        },
        CancelTopping: {
          id: 'TOPPING_SESSION',
          data: 'UNTOP'
        },
        Hide: {
          id: 'HIDE_SESSION',
        }
      }
      this.$track.clickStat(this.$track.formatParams(trackObj[type].id, trackObj[type].data));
    },
  },
};
</script>

<style scoped lang='scss'>
.inside-im-box {
  height: 100%;
  .search_div {
    margin: 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .search_input {
      width: 324px;
    }
    .iconfont {
      font-size: 16px;
      cursor: pointer;
    }
  }
  .wrap {
    display: block;
    height: calc(100% - 56px);
    overflow-y: scroll;
    box-sizing: border-box;
    padding-bottom: 10px;
    .customer-item {
      position: relative;
      padding: 12px 0;
      // border: 1px solid #fff;
      border-left: 3px solid #fff;
      background: #ffffff;
      margin-bottom: 1px;

      .icon-close {
        width: 12px;
        height: 12px;
        display: none;
        font-size: 12px;
        color: #B9BFBD;
        position: absolute;
        left: 4px;
        top: 50%;
        transform: translate(0, -50%);
      }
      .icon {
        vertical-align: initial;
        border-radius: 50%;
      }
      .Topping {
        width: 0;
        height: 0;
        position: absolute;
        right: 0;
        top: 0;
        border-top: 8px solid #bde7ff;
        border-right: 8px solid #bde7ff;
        border-bottom: 8px solid transparent;
        border-left: 8px solid transparent;
      }
      img {
        width: 40px;
        height: 40px;
        margin-left: 20px;
      }
      .item-info {
        position: relative;
        font-size: 14px;
        line-height: 22px;
        margin-left: 6px;
        .im-name {
          display: flex;
          justify-content: space-between;
          align-items: center;
          width: 286px;
          .user-name {
            max-width: 186px;
            // width: 100%;
            @include text-ellipsis();
          }

          .im-time {
            font-size: 12px;
            font-weight: 400;
            color: #B9BFBD;
          }
          position: absolute;
          height: 22px;
          color: #000000;
          top: -48px;
          left: 64px;
          .tag-box {
            display: flex;
            flex-direction: column;
            position: absolute;
            right: 10px;
          }
          .tag {
            margin-left: 4px;
            font-size: 12px;
            height: 20px;
            display: flex;
            align-items: center;
            span {
              transform: scale(0.9);
              display: inline-block;
            }
            &.tag-ygq {
              color: #ff9100;
              background: #fff3e0;
              border-radius: 10px;
              border: 1px solid #ffe0b2;
              padding: 2px 6px;
            }
            &.tag-yjs {
              color: #ff4d4f;
              background: #fff1f0;
              border-radius: 10px;
              border: 1px solid #ffccc7;
              padding: 2px 6px;
              margin-bottom: 5px;
            }
          }
        }
        .im-content {
          position: absolute;
          height: 22px;
          color: #8c8c8c;
          left: 64px;
          top: -24px;
          font-size: 12px;
          @include text-ellipsis;
          max-width: 260px;
        }
        .im-delete {
          display: none;
          i {
            font-size: 10px;
            position: absolute;
            top: -36px;
            margin-top: 5px;
            margin-left: 3px;
            color: #000;
            opacity: 0.25;
            left: -7px;
          }
        }
      }
      .im-msg {
        color: #fff;
        font-size: 12px;
        text-align: center;
        line-height: 18px;
        height: 18px;
        background: #f86137;
        position: absolute;
        top: 30px;
        right: 10px;
      }
      .im-small-count {
        width: 18px;
        border-radius: 50%;
      }

      .im-medium-count {
        width: 24px;
        border-radius: 10px;
      }
      .im-big-count {
        width: 36px;
        border-radius: 10px;
      }
    }
    .customer-item:hover .im-delete {
      display: block;
    }
    .customer-item:hover .icon-close {
      display: block;
    }
    .customer-item:hover,
    .addColor {
      cursor: pointer;
      background: #f6f6f6;
      // border: 1px solid #eee;
      border-left: 3px solid #f6f6f6;
    }
    .addColor:hover {
      border-left: 3px solid $color-primary-light-6;
    }
    .addColor {
      border-left: 3px solid $color-primary-light-6;
    }
  }
  .default {
    margin-top: 100px;
    .default-img {
      text-align: center;
      img {
        width: 88px;
        height: 92px;
      }
    }
    .default-msg {
      width: 100%;
      text-align: center;
    }
  }
  .Operation {
    display: flex;
    height: 100%;
    position: absolute;
    right: 18px;
    top: -52px;
    flex-wrap: wrap;
    justify-content: center;
    margin-bottom: 9px;
    .Operations {
      width: 100%;
      text-align: center;
      margin-bottom: 8px;
      color: #8c8c8c;
    }
    .Operations:hover {
      color: $color-primary-light-6;
    }

    .OperationLists {
      width: 200px;
    }
  }
}
</style>
